package com.cqc.customslidmenu01;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by a on 2018/3/13.
 * 侧滑
 * <p>
 * getScrollX()  View的left  x轴的值
 */

public class SlideView extends ViewGroup {

    private int downX;
    private Scroller scroller;

    private int LEFT_MENU_STATE = 0;
    private int Content_STATE = 1;
    private int CURRENT_STATE = 1;

    public SlideView(Context context) {
        this(context, null);
    }

    public SlideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scroller = new Scroller(context);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        View menuLeftView = getChildAt(0);
        //onMeasure后才可以获取View的宽高，再此之前用layoutParams
        int menuWidth = menuLeftView.getMeasuredWidth();
        menuLeftView.measure(menuLeftView.getLayoutParams().width, heightMeasureSpec);


        View contentView = getChildAt(1);
        contentView.measure(widthMeasureSpec, heightMeasureSpec);

        LogUtil.d("onMeasure,menuWidth=" + menuWidth + ",widthMeasureSpec=" + widthMeasureSpec + ",heightMeasureSpec=" + heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        View menuLeftView = getChildAt(0);
        int menuWidth = menuLeftView.getMeasuredWidth();
        menuLeftView.layout(-menuWidth, 0, 0, b);


        View contentView = getChildAt(1);
        contentView.layout(l, t, r, b);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
//                LogUtil.d("down,getScrollX()=" + getScrollX());
                break;

            case MotionEvent.ACTION_MOVE:
                int moveX = (int) event.getX();
                int instance = downX - moveX;

                int scrollX = getScrollX() + instance;//因为getScrollX()表示的是上一次的滑动距离，而downX - moveX才是当前的滑动的距离

                LogUtil.d("move,getScrollX()=" + getScrollX() + ",ownX=" + downX + ",moveX=" + moveX);

                if (scrollX < -getChildAt(0).getWidth()) {
                    scrollTo(-getChildAt(0).getWidth(), 0);
                } else if (scrollX > 0) {
                    scrollTo(0, 0);
                } else {
                    /** 为什么是scrollBy(instance, 0);，而不是 scrollBy(scrollX,0)
                     * getScrollX 表示 down点的坐标-up点的坐标,移动的过程中，第一次移动的getScrollX=0，
                     * 如果是scrollBy(scrollX,0),那么虽然你在滑动，但是第一个点scrollBy(0,0),导致始终走不出第一步，所以无法移动
                     */
                    scrollBy(instance, 0);
                }
                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                //手指移开后判断该 打开/关闭侧拉栏，依据是滑动的距离<>侧拉栏的1/2
                int center = getChildAt(0).getWidth() / 2;
                int currentScrollX = getScrollX();
                LogUtil.d("currentScrollX=" + currentScrollX);
                //getScrollX 表示 down点的坐标-up点的坐标,移动的过程中 getScrollXd的值得变化范围[0,downX-upX],
                if (Math.abs(currentScrollX) > center) {
                    //切换到右边
                    LogUtil.d("切换到右边");
                    CURRENT_STATE = LEFT_MENU_STATE;
                } else {
                    //切换到左边
                    LogUtil.d("切换到左边");
                    CURRENT_STATE = Content_STATE;
                }
                switchPager();
                break;
        }
        return true;
    }

    private void switchPager() {
        int startX = getScrollX();
//        LogUtil.d("startX="+startX);
        int dx = 0;
        if (CURRENT_STATE == LEFT_MENU_STATE) {
            scrollTo(-getChildAt(0).getWidth(), 0);
//            dx = -getChildAt(0).getWidth() - startX;
        } else {
            scrollTo(0, 0);
//            dx = -0 - startX;
        }

//        scroller.startScroll(startX, 0, dx, 0, Math.abs(dx) * 5);
//        invalidate();
    }

//    @Override
//    public void computeScroll() {
//        // 当scroller模拟完毕数据时，不再调用invalidate
//        if (scroller.computeScrollOffset()) {
//            int currX = scroller.getCurrX();
//            scrollTo(currX, 0);
//            invalidate();
//        }
//    }
}
